package com.itheima.controller;


import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Permission;
import com.itheima.service.CheckItemService;
import com.itheima.service.PermissionService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/*
检查项管理
 */
@RestController
@RequestMapping("/permission")
public class PermissionSettingController {
    @Reference
    private PermissionService permissionService;

    //新增检查项
    @RequestMapping("/add")
    public Result add(@RequestBody Permission permission) {
        try {
            permissionService.add(permission);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.ADD_MEMBER_PERMISSION_SETTING_FAIL);
        }
        return new Result(true,MessageConstant.ADD_MEMBER_PERMISSION_SETTING_SUCCESS);
    }

    //检查项分页查询
    @RequestMapping("/findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = permissionService.pageQuery(queryPageBean);
        return pageResult;
    }

    //检查项删除
    @PreAuthorize("hasAnyAuthority('CHECKITEM_DELETE')")  //权限校验
    @RequestMapping("/delete")
    public Result delete(Integer id) {
        try {
            permissionService.deleteById(id);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.DELETE_MEMBER_PERMISSION_SETTING_FAIL);
        }
        return new Result(true,MessageConstant.DELETE_MEMBER_PERMISSION_SETTING_SUCCESS);
    }


    //查询检查项
    @RequestMapping("/findById")
    public Result findById(Integer id) {
        try {
            Permission permission = permissionService.findById(id);
            return new Result(true,MessageConstant.GET_MEMBER_PERMISSION_SETTING_SUCCESS,permission);
        } catch (Exception e) {
            e.printStackTrace();
                return new Result(false, MessageConstant.GET_MEMBER_PERMISSION_SETTING_FAIL);
        }
    }
    //检查组里的查询所有的检查项
    @RequestMapping("/findAll")
    public Result findAll() {
        try {
            List<Permission> findAll = permissionService.findAll();
            return new Result(true,MessageConstant.GET_MEMBER_PERMISSION_SETTING_SUCCESS,findAll);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, MessageConstant.GET_MEMBER_PERMISSION_SETTING_FAIL);
        }

    }
}
